import { _decorator, Component, director, EventTouch, instantiate, math, Node, Prefab, Vec3 } from 'cc';
import { EventManager, MyEvent } from './EventManager';
const { ccclass, property } = _decorator;

@ccclass('GameManager')
export class GameManager extends Component {

    public static Level: number = 0;

    @property(Node)
    Level: Node = null;

    @property(Prefab)
    LevelPrefabs: Prefab[] = [];

    @property(Node)
    PausePanel: Node = null;

    protected onLoad(): void {
        this.initLevel();
    }

    initLevel() {
        const index: number = math.clamp(GameManager.Level, 0, this.LevelPrefabs.length - 1);
        const level: Node = instantiate(this.LevelPrefabs[index]);
        level.parent = this.Level;
        level.setPosition(Vec3.ZERO);
    }

    OnButtonClick(target: EventTouch) {
        switch (target.getCurrentTarget().name) {
            case "Pause":
                this.PausePanel.active = true;
                EventManager.emit(MyEvent.PAUSE);
                break;
            case "Resume":
                this.PausePanel.active = false;
                EventManager.emit(MyEvent.PAUSE);
                break;
            case "Back":
                director.loadScene("menu");
                break;
        }
    }
}


